package com.kx.center.controller;

import java.util.HashMap;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.kx.center.enumerate.MessageCode;
import com.kx.center.service.GoodsService;
import com.kx.center.util.Response;

@Controller
@RequestMapping("/goods")
public class GoodsController extends CommonController{

	@Autowired
	private GoodsService service;

	@RequestMapping(value = "list/{storeId}/{flag}", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String list(HttpServletRequest request,
			@PathVariable("storeId") Integer storeId,
			@PathVariable("flag") boolean flag) throws JsonProcessingException {
		/*
		 * 名称: 商品列表 地址: /api/goods/list 返回: [{goods_id:1, goods_sku: 'xx0001',
		 * goods_name: 'abcabab',storage: 123},{goods_id:2, goods_sku: 'xx0002',
		 * goods_name: 'abcabab', storage: 123}] 出错返回: {status: 'error', code:
		 * 100001, msg: '发生错误'}
		 */
		if (storeId > 0) {
			String json = "";
			json = service.getJsonGoods(storeId, flag);

			return json;
		} else {
			return Response.error(MessageCode.NO_DATA);
		}
	}

	@RequestMapping(value = "type/list", method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
	@ResponseBody
	public String types() {
		List<HashMap<String, Object>> types = service.findAllGoodsType();
		HashMap<String, Object> map = new HashMap<String, Object>();
		map.put("data", types);
		return Response.success(map);
	}

}
